package com.necsv;

import Util.ColorBallDrag;
import android.R;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Point;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class DrawViewDrag extends View{

	
	private static final String TAG = "DrawViewDrag";
	private Context context;
	ColorBallDrag[] Balls = new ColorBallDrag[2];
	private int BallID;

	public DrawViewDrag(Context context) {
		super(context);
		this.context = context;
		// TODO Auto-generated constructor stub
		setFocusable(true);
		Point p = new Point();
		p.x = 50;
		p.y = 20;
		
		Point p2 = new Point();
		p2.x = 150;
		p2.y = 20;
		
		Balls[0] = new ColorBallDrag(context,com.necsv.R.drawable.bol_rood , p);
		Balls[1] = new ColorBallDrag(context,com.necsv.R.drawable.bol_geel , p2);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		for (ColorBallDrag ball : Balls) {
			canvas.drawBitmap(ball.getImg(), ball.getDx(), ball.getDy(), null);
		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		int eventaction = event.getAction();
		int X = (int) event.getX();
		int Y = (int) event.getY();
		switch (eventaction) {
		case MotionEvent.ACTION_DOWN:
			BallID = 0;
			Log.v(TAG, "action down");
			for (ColorBallDrag ball : Balls) {
				int centerX = ball.getDx() + 25;
				int centerY = ball.getDy() + 25;
				double radCircle = Math
						.sqrt((double) (((centerX - X) * (centerX - X)) + (centerY - Y) * (centerY - Y)));

				// if the radius is smaller then 23 (radius of a ball is 22),
				// then it must be on the ball
				if (radCircle < 23) {
					BallID = ball.getId();
					break;
				}
			}
			break;
		case MotionEvent.ACTION_MOVE:
			if(BallID > 0)
			{
				Balls[BallID -1].setDx(X - 25);
				Balls[BallID -1].setDy(Y - 25);
			}
			break;
		case MotionEvent.ACTION_UP:
			break;

		default:
			break;
		}
		
		invalidate();
		return true;
	}

}
